const express = require('express')
const fs = require('fs')

const app = express();

const ip = require("ip");
const IP = ip.address();
console.log(IP);

// 1.渲染器
const {createBundleRenderer} = require('vue-server-renderer');
const bundle = require('../dist/server/vue-ssr-server-bundle.json')

app.use(express.static('../dist/client', {index: false}))

// bundle是服务端包
const renderer = createBundleRenderer(bundle, {
    runInNewContext: false,
    //同步式 阻塞式加载 文件
    template: fs.readFileSync('../public/index.tmpl.html', "utf-8"),
    clientManifest: require('../dist/client/vue-ssr-client-manifest.json')
})

app.get('*', async function(req, res) {
    console.log(req.url);
    
    const context = {
        title: 'SSR Test',
        url: req.url
    }

    // 2.执行渲染
    const html = await renderer.renderToString(context)
    res.send(html);
})

const server = app.listen(3000, () => {
    let port = server.address().port;
    console.log('服务器准备就绪了');
    console.log('','http://'+IP+':'+port);
})